package org.ly2048.com.ly2048.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import org.ly2048.com.ly2048.R;
import org.ly2048.com.ly2048.snake.SnakeView;

public class SnakeActivity extends Activity {
	private Vibrator vibrator;
	private SnakeView snakeView;
	private TextView numText;
	private AlertDialog mAlertDialog;
	private float downX, downY;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_snake);
		snakeView = (SnakeView) findViewById(R.id.snakeView1);
		numText = (TextView) findViewById(R.id.num);
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		mAlertDialog = new AlertDialog.Builder(SnakeActivity.this).setTitle("本局结束")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {// 添加确定按钮

					@Override

					public void onClick(DialogInterface dialog, int which) {// 确定按钮的响应事件

					}

				}).create();
		snakeView.setOnEatSome(new SnakeView.EatSome() {

			@Override
			public void onEat(int num) {
				numText.setText("score: " + num);
				Log.e("当前得分", "score: " + num);
			}
		});
		snakeView.setOnStatusListener(new SnakeView.OnStatusListener() {

			@Override
			public void onStop(int num) {
				numText.setText("score: " + 0);
				vibrator.vibrate(500);
				mAlertDialog.setMessage("本局得分:" + num);
				mAlertDialog.show();
			}
		});
	}

	public void speed(View view) {
		snakeView.speed();
	}

	public void start(View view) {
		snakeView.start();
	}

	public void pause(View view) {
		snakeView.pause();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				downX = event.getX();
				downY = event.getY();
				break;
			case MotionEvent.ACTION_UP:
				float dX = event.getX() - downX;
				float dY = event.getY() - downY;
				if (Math.abs(dX) < 50 && Math.abs(dY) < 50) {
					return super.onTouchEvent(event);
				}
				Move(getOrientation(dX, dY));
				break;
			default:
				break;
		}
		return super.onTouchEvent(event);
	}

	private char getOrientation(float dX, float dY) {
		if (Math.abs(dX) > Math.abs(dY)) {
			if (dX > 0) {//右移
				return 'r';
			} else {//左移
				return 'l';
			}
		} else {
			if (dY > 0) {//下移
				return 'd';
			} else {//上
				return 't';
			}
		}
	}

	private void Move(char orientation) {
		switch (orientation) {
			case 'l':
				snakeView.left();
				break;
			case 'r':
				snakeView.right();
				break;
			case 't':
				snakeView.up();
				break;
			case 'd':
				snakeView.down();
				break;
			default:
				break;
		}
	}

	@Override
	protected void onDestroy() {
		if (mAlertDialog != null) {
			mAlertDialog.dismiss();
		}
		snakeView.onDestroy();
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
